<?php
class BlogFormatter extends CFormatter {

	public $dateFormat='Y-m-d H:i:s';

	protected static $_instance = null;
	
	public function formatPostStatus($value) {
		$data = Post::model()->getStatusListData();
		return isset($data[$value]) ? $data[$value] : '';
	}
	
	public function formatPostVisibility($value) {
		$data = Post::model()->getVisibilityListData();
		return isset($data[$value]) ? $data[$value] : '';
	}

	public function formatShortDate($value) {
		$t = strtotime($value);
		return date('Y M d', $t);
	}

	public function formatDate($value) {
		$t = strtotime($value);
		return date('Y M d - l, g:i A', $t);
	}
	
	public static function getInstance() {
		if (!self::$_instance)
			self::$_instance = new BlogFormatter();
		return self::$_instance;
	}
	
	public function processPostContent($content) {
		$result = $content;
		
		// replace image with place holder
		$pattern = '/<img(.*?)src="([^"]*)"/i';
		$img = Yii::app()->theme->baseUrl.'/img/img-loading.gif';
		$replacement = '<img${1}src="'.$img.'" data-src="${2}"';
		$result = preg_replace($pattern, $replacement, $result);
		
		// add target blank to anchor
		$pattern = '/<a (.*?)>/i';
		$replacement = '<a target="_blank" ${1}>';
		$result = preg_replace($pattern, $replacement, $result);
		
		// remove img inline style
		$pattern = '/<img(.*?)style=".*?"(.*?)>/i';
		$replacement = '<img${1}${2}>';
		$result = preg_replace($pattern, $replacement, $result);

		return $result;
	}
}